import { defineStore } from "pinia";
import type { SysUserVo, UserLoginDto } from "@/api";
import { Client } from "@/api/clientApi";
import { computed, ref } from "vue";
import { useTabsStore } from "@/stores/tabs";
import router from "@/router";

export enum FormStatus {
  LoginForm,
  RegisterForm,
}

export const useUserStore = defineStore(
  "user",
  () => {
    const userInfo = ref<SysUserVo>();
    const token = ref<string>();
    const formStats = ref<FormStatus>(FormStatus.LoginForm);
    const getStatus = computed(() => formStats.value);

    const tabsStore = useTabsStore();

    /**
     * 登录
     * @param user
     */
    async function login(user: UserLoginDto) {
      const response = await Client.systemSysUser.postSysUserLogin(user);
      token.value = response.token;
      userInfo.value = response.sysUserVo;
      tabsStore.clearTabs();
      await router.push("/");
    }

    /**
     * 退出登录
     */
    async function logout() {
      // await Client.systemSysUser.postSysUserLogout();
      // token.value = undefined;
      // userInfo.value = undefined;
    }

    async function register() {}

    /**
     * 切换登录
     */
    function switchLogin() {
      formStats.value = FormStatus.LoginForm;
    }

    /***
     * 切换注册
     */
    function switchRegister() {
      formStats.value = FormStatus.RegisterForm;
    }

    function clearInfo() {
      token.value = undefined;
      userInfo.value = undefined;
    }

    return {
      userInfo,
      login,
      getStatus,
      switchLogin,
      switchRegister,
      clearInfo,
      token,
    };
  },
  {
    persist: true,
  }
);

// export const useUserStoreWithOut = store();
